package com.luom.fsp.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luom.fsp.common.ErrorCode;
import com.luom.fsp.constant.CommonConstant;
import com.luom.fsp.exception.BusinessException;
import com.luom.fsp.exception.ThrowUtils;
import com.luom.fsp.mapper.CarouselMapper;
import com.luom.fsp.model.dto.carousel.CarouselQueryRequest;
import com.luom.fsp.model.entity.Carousel;
import com.luom.fsp.model.vo.CarouselVO;
import com.luom.fsp.service.CarouselService;
import com.luom.fsp.service.UserService;
import com.luom.fsp.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 优惠券表服务实现
 *
*/
@Service
@Slf4j
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

    @Resource
    private UserService userService;

    /**
     * 校验数据
     *
     * @param carousel
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validCarousel(Carousel carousel, boolean add) {

        ThrowUtils.throwIf(carousel == null, ErrorCode.PARAMS_ERROR);
        Long id = carousel.getId();
        String imageUrl = carousel.getImageUrl();
        Integer isUsed = carousel.getIsUsed();
        Date createTime = carousel.getCreateTime();
        Integer isDelete = carousel.getIsDelete();

        if (add){
            if (isUsed==null){
                carousel.setIsUsed(0);
            }
            if (ObjectUtils.isEmpty(imageUrl)){
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"图片地址不能为空");
            }
        }

        // 修改数据时，有参数则校验
        if (ObjectUtils.isNotEmpty(isUsed)) {
            ThrowUtils.throwIf(!isUsed.equals(0) && !isUsed.equals(1), ErrorCode.PARAMS_ERROR, "是否使用错误");
        }
    }

    /**
     * 获取查询条件
     *
     * @param carouselQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Carousel> getQueryWrapper(CarouselQueryRequest carouselQueryRequest) {
        QueryWrapper<Carousel> queryWrapper = new QueryWrapper<>();
        if (carouselQueryRequest == null) {
            return queryWrapper;
        }

        Long id = carouselQueryRequest.getId();
        Integer isUsed = carouselQueryRequest.getIsUsed();
        String sortField = carouselQueryRequest.getSortField();
        String sortOrder = carouselQueryRequest.getSortOrder();

        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(isUsed), "isUsed", isUsed);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取优惠券表封装
     *
     * @param carousel
     * @param request
     * @return
     */
    @Override
    public CarouselVO getCarouselVO(Carousel carousel, HttpServletRequest request) {
        // 对象转封装类
        return CarouselVO.objToVo(carousel);
    }

    /**
     * 分页获取优惠券表封装
     *
     * @param carouselPage
     * @param request
     * @return
     */
    @Override
    public Page<CarouselVO> getCarouselVOPage(Page<Carousel> carouselPage, HttpServletRequest request) {
        List<Carousel> carouselList = carouselPage.getRecords();
        Page<CarouselVO> carouselVOPage = new Page<>(carouselPage.getCurrent(), carouselPage.getSize(), carouselPage.getTotal());
        if (CollUtil.isEmpty(carouselList)) {
            return carouselVOPage;
        }
        // 对象列表 => 封装对象列表
        List<CarouselVO> carouselVOList = carouselList.stream().map(CarouselVO::objToVo).collect(Collectors.toList());

        carouselVOPage.setRecords(carouselVOList);
        return carouselVOPage;
    }

}
